package com.example.salesservice.web;

import com.example.salesservice.entity.Sales;
import com.example.salesservice.entity.Salesback;
import com.example.salesservice.service.SalesService;
import com.example.salesservice.service.SalesbackService;
import com.example.salesservice.utils.LayuiJson;
import com.example.salesservice.utils.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/salesback")
public class SalesbackController {
    @Autowired
    private LayuiJson layuiJson;
    @Autowired
    private SalesbackService salesbackService;
    @Autowired
    private SalesService salesService;

    /**
     * 查询所有退货订单信息（或根据条件进行查询）
     * @return
     */
    @RequestMapping("/loadAllSalesback")
    @ResponseBody
    public LayuiJson<Salesback> loadAllSalesback(Integer customerid,Integer goodsid,Integer page,Integer limit){
        //设置默认值
        page = page == null ? 1 : page;
        limit = limit == null ? 5 : limit;
        //根据前端发送页码数据设置参数起始查询条件（默认为0）
        int index=(page-1)*limit;
        List<Salesback> data = salesbackService.select(customerid, goodsid, index, limit);
        layuiJson.setCode(0);
        layuiJson.setCount(1000);
        layuiJson.setData(data);
        return layuiJson;
    }

    /**
     * 根据退货订单id删除该order 数据库条目
     * @return 操作执行结果
     */
    @RequestMapping("deleteSalesback")
    @ResponseBody
    public ResultObj deleteSalesback(Integer id){
        try {
            salesbackService.delete(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 根据销售页面 对应不同订单的退货选项，返回对应订单详情及退货信息
     * 判断是否符合退货标准
     * @return 操作执行结果
     */
    @Transactional
    @RequestMapping("/addSalesback")
    @ResponseBody
    public ResultObj addSalesback(Integer id ,Integer number,String remark){
        //根据传入订单id获取sales信息
        Sales sales = salesService.selectById(id);
        //获取当前时间及账号信息
        Date salesbacktime = new Date();
        //String operateperson = (String) httpSession.getAttribute("username");
        String operateperson = "pzh";

        //判断退货数量是否符合要求（1、部分退货，2、完全退货，3、超出货物上线）
        if(number < sales.getNumber()){
            salesbackService.insert(sales.getCustomerid(), sales.getPaytype(), salesbacktime,
                    sales.getSaleprice(), operateperson, number, remark, sales.getGoodsid());
            salesService.updateNumber(id, sales.getNumber()-number);

            return ResultObj.BACKINPORT_SUCCESS;
        }else if(number.equals(sales.getNumber())){
            salesbackService.insert(sales.getCustomerid(), sales.getPaytype(), salesbacktime,
                    sales.getSaleprice(), operateperson, number, remark, sales.getGoodsid());
            salesService.delete(id);

            return ResultObj.BACKINPORT_SUCCESS;
        }else{
            return ResultObj.BACKINPORT_ERROR;
        }
    }
}
